//
//  DDWoIntegralVC.m
//  dingdang
//
//  Created by 阮 沧晖 on 15/7/18.
//  Copyright (c) 2015年 阮 沧晖. All rights reserved.
//

#import "DDWoIntegralVC.h"
#import "HTHorizontalSelectionList.h"
#import "DDDonationVC.h"
#import "DDConsumptionListVC.h"
#import "DDNetWorkAPIClient.h"
#import "DDIntegralExchange.h"
@interface DDWoIntegralVC ()<HTHorizontalSelectionListDelegate, HTHorizontalSelectionListDataSource>
{
    DDConsumptionListVC * listVC1;
    DDConsumptionListVC * listVC2;
}
@property (weak, nonatomic) IBOutlet UILabel *intgeralLabel;

//tabbar
@property (nonatomic, strong) HTHorizontalSelectionList *selectionList;
@property (nonatomic, strong) NSArray *tabMakes;
@property (weak, nonatomic) IBOutlet UIView *tabSelectView;
@property (weak, nonatomic) IBOutlet UIView *tabSelectViewTop;

@end

@implementation DDWoIntegralVC

- (void)viewDidLoad {
    NSLog(@"进入到---------->DDWoIntegralVC");
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self createUI];
    [self createData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)createUI
{
    self.view.frame = [UIScreen mainScreen].bounds;
    _tabSelectView.frame = [UIScreen mainScreen].bounds;
    self.view.backgroundColor = RGB(245, 245, 245);
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBarHidden = YES;
    //自定义TAB BAR UI
    [self addNavigatorTitle:NSLocalizedString(@"我的积分", nil) parent:self.view];
    self.barViewControl.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"top_Background"]];
    [self AddLeftBtnAction:nil normal:@"top_返回" selected:@"top_返回" action:^{
        //[self dismissViewControllerAnimated:YES completion:nil];
        [self.navigationController popViewControllerAnimated:YES];
    }];
    
    
    
    //tab bar
    self.selectionList = [[HTHorizontalSelectionList alloc] initWithFrame:CGRectMake(0, 0, self.tabSelectView.frame.size.width, 44)];
    self.selectionList.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    self.selectionList.delegate = self;
    self.selectionList.dataSource = self;
    self.selectionList.HTHorizontalSelectionListInternalPadding = 40;
    self.selectionList.HTHorizontalSelectionListHorizontalMargin = self.view.frame.size.width/4;
    self.selectionList.backgroundColor = [UIColor whiteColor];
    self.selectionList.selectionIndicatorColor = RGB(42, 172, 226);

    [self.selectionList setTitleColor:[UIColor colorWithRed:132.0/255.0 green:132.0/255.0 blue:132.0/255.0 alpha:1.0] forState:UIControlStateNormal];
    [self.selectionList setTitleColor:RGB(42, 172, 226) forState:UIControlStateSelected];
    
    NSMutableArray *buttonArray = [[NSMutableArray alloc] init];
    self.tabMakes = @[@"获取记录",
                      @"消费记录"
                     ];
    for (NSString *title in self.tabMakes) {
        UIButton *titleButton = [self.selectionList selectionListButtonWithTitle:title];
        [buttonArray addObject:titleButton];
    }
    self.tabMakes = buttonArray;
    [self.tabSelectViewTop addSubview:self.selectionList];
    
    listVC1 =  [[DDConsumptionListVC alloc] initWithNibName:@"DDConsumptionListVC" bundle:nil];
    listVC1.logType = @"1";
    //listVC1.view.frame = self.view.bounds;
    listVC1.view.frame =  CGRectMake(0,50, [UIScreen mainScreen].bounds.size.width,[UIScreen mainScreen].bounds.size.height-203);
    listVC1.view.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin;
    [self addChildViewController:listVC1];

    listVC1.view.autoresizingMask = UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleTopMargin;
    [self.tabSelectView addSubview:listVC1.view];

    
    listVC2 =  [[DDConsumptionListVC alloc] initWithNibName:@"DDConsumptionListVC" bundle:nil];
    listVC2.logType = @"2";
    listVC2.view.backgroundColor = [UIColor whiteColor];
    //listVC2.view.frame = CGRectMake(0,0, listVC2.view.frame.size.width, self.view.frame.size.height - 300);
    //listVC2.view.frame = CGRectMake(0, 0, self.view.frame.size.width, 600);
    //listVC2.view.frame = CGRectMake(0, 0,  self.tabSelectView.frame.size.width,  self.tabSelectView.frame.size.height);
    listVC2.view.frame =  CGRectMake(0,50, [UIScreen mainScreen].bounds.size.width,[UIScreen mainScreen].bounds.size.height-203);
    //listVC2.view.autoresizingMask = UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleTopMargin;
    [self addChildViewController:listVC2];
}

-(void)createData
{
//    DDNetWorkAPIClient *network = [[DDNetWorkAPIClient alloc]init];
//    [network giftQuery:@{@"logType":@"2"} completionBlock:^(BOOL isSuccess, NSDictionary *dict, NSString *message, NSString *status) {
//        NSLog(@"%@",dict);
//    }];
    
    _intgeralLabel.text =[NSString stringWithFormat:@"我累积的积分：%@",_intgeral];
}

- (void)viewWillAppear:(BOOL)animated
{
    DDNetWorkAPIClient *network = [[DDNetWorkAPIClient alloc] init];
    [network queryIntegral:^(BOOL isSuccess, NSDictionary *dict, NSString *message, NSString *status) {
        NSLog(@"%@",dict);
        if (isSuccess) {
            _intgeral = [[dict objectForKey:@"integral"]stringValue];
            _intgeralLabel.text =[NSString stringWithFormat:@"我累积的积分：%@",_intgeral];
        }
    }];
}


#pragma mark - HTHorizontalSelectionListDataSource Protocol Methods

- (NSInteger)numberOfItemsInSelectionList:(HTHorizontalSelectionList *)selectionList {
    return self.tabMakes.count;
}

- (UIButton *)selectionList:(HTHorizontalSelectionList *)selectionList titleForItemWithIndex:(NSInteger)index {
    return self.tabMakes[index];
}

#pragma mark - HTHorizontalSelectionListDelegate Protocol Methods

- (void)selectionList:(HTHorizontalSelectionList *)selectionList didSelectButtonWithIndex:(NSInteger)index {
    // update the view for the corresponding index
    //self.selectedItemLabel.text = self.carMakes[index];
    if (index == 0) {
        [listVC1.view removeFromSuperview];
        [listVC1 removeFromParentViewController];
        [self addChildViewController:listVC1];
        [self.tabSelectView addSubview:listVC1.view];
    }
    else if (index == 1)
    {
        [listVC2.view removeFromSuperview];
        [self addChildViewController:listVC2];
        [self.tabSelectView addSubview:listVC2.view];
    }
}
- (IBAction)donationAction:(id)sender {
    DDDonationVC *donationVC = [[DDDonationVC alloc] initWithNibName:@"DDDonationVC" bundle:nil];
    
    [self.navigationController pushViewController:donationVC animated:YES];
    
}
- (IBAction)goShopAction:(id)sender {
    DDIntegralExchange *gift = [[DDIntegralExchange alloc]initWithNibName:@"DDIntegralExchange" bundle:nil];
    [self.navigationController pushViewController:gift animated:YES];
}

@end
